<?php
namespace app\applet\model;

use app\common\model\Applet as AppletModel;
use think\facade\Session;

/**
 * 小程序模型
 */
class Applet extends AppletModel
{
    /**
     * 小程序登录
     */
    public function login($data)
    {
        if(!captcha_check($data['captcha'])){
            $this->error = '验证码错误';
            return false;
        }
        if(!$applet = Applet::get($data['user_name'])){
            $this->error = '小程序不存在';
            return false;
        }
        if(empty($applet['password'])){
            $this->error = '通过用户中心一键登录，初始化密码后在登录！';
            return false;
        }else{
            if($applet['password'] != hema_hash($data['password'])){
                $this->error = '密码错误';
                return false;
            }
        }
		$user = User::getUser(['user_id' => $applet['user_id']]);
        Session::set('hema_applet',[
        	'user' => $user,
        	'applet' => $applet,
        	'is_login' => true,
        ]);
        return true;
    }
    
    /**
     * 修改登录密码
     */
    public function renew(array $data)
    {   
        if(strlen($data['password']) < 6){
            $this->error = '密码长度不足6位';
            return false;
        }
        if ($data['password'] != $data['password_confirm']) {
            $this->error = '两次密码输入不一致';
            return false;
        }
        $data['password'] = hema_hash($data['password']);
        return $this->save($data) !== false;
    }
}